library(xtable)

fifteen <- function(mydata,threshold){
  m <- c(1,(1:1430)[agree$agree==1],length(mydata)+1)
  b <- c()
  for (i in 1:(length(m)-1)){
    a <- NA 
    a <- rep(i,(m[i+1]-m[i]))
    b <- c(b,a)
  }
  bir <- tapply(mydata, b, mean)
  n <- c(1,(1:1430)[agree$agree==1]-threshold,length(mydata)+1) 
  th <- c()
  for(i in 2:(length(m)-1)){
    bos <- c()
    bos <- mydata[n[i]:m[i]]
    th <- c(th, bos)
  }
  iki <- tapply(th, rep(1:15, each=threshold+1), mean)
  result <- data.frame("all"=bir[-16],"threshold"=iki)
  return(result)
}

one <- fifteen(civil.as.op$d,15)
two <- fifteen(civil.op.as$d,15)
three <- fifteen(civil.as.is$d,15)
four <- fifteen(civil.is.as$d,15)
five <- fifteen(civil.sdf.is$d,15)
six <- fifteen(civil.is.sdf$d,15)

mydt <- data.frame(one,two,three,four,five,six)
xtable(mydt)

###################
##
## The code below generates shaded areas for 15 days before meeting dates
##
###################

div.plotting <- function(mydata, divdata, area, time, lower, upper,y){
  ggplot(mydata, aes(x=time, y=d)) +
    geom_line() +
    sapply(mydata$date[(1:1430)[agree$agree==1]], function(xint2) geom_rect(data= data.frame(xmin = xint2-15, xmax = xint2-1, ymin = -140, ymax = 110), aes(xmin=xmin,ymin=ymin,xmax=xmax,ymax=ymax,fill="15 Days"), alpha=0.5,inherit.aes=FALSE)) +
    sapply(mydata$date[(1:1430)[agree$agree==1]], function(xint) geom_line(linetype = "dashed",inherit.aes=FALSE, data = data.frame(x = c(xint,xint), y = c(-140, 110)), aes(x = x, y = y,color="Meeting"))) +
    ggtitle(area) +  theme_classic() + 
    scale_y_continuous(name=y, limits=c(lower, upper)) + 
    labs(x = " ", y = y, color = " ", size=0.8) + 
    scale_fill_manual(name="", values = c("15 Days"="grey")) +
    scale_color_manual(name="", values = c("Meeting"="skyblue2")) +
    theme(plot.title = element_text(size = 16, family = "LM Roman 10"), 
          axis.text=element_text(size=10, family = "LM Roman 10"),
          axis.title=element_text(size=12, family = "LM Roman 10"),
          legend.text=element_text(size=12, family = "LM Roman 10"),
          legend.title=element_text(size=12, family = "LM Roman 10"), legend.direction = "horizontal", 
          legend.position = "bottom",
          legend.box = "horizontal")
}

p.as.op <- div.plotting(civil.as.op,divisive.as.op,"Opposition Territory",time=civil.as.op$date,-140,110, "Government-Inflicted") + theme(legend.position = "none")
p.op.as <- div.plotting(civil.op.as,divisive.op.as,"Government Territory",time=civil.op.as$date,-140,110,"Opposition-Inflicted")+theme(legend.direction = "horizontal")

p.as.is <- div.plotting(civil.as.is,divisive.as.is,"ISIS Territory",time=civil.as.is$date,-140,110, "Government-Inflicted") + theme(legend.position = "none")
p.is.as <- div.plotting(civil.is.as,divisive.is.as,"Government Territory",time=civil.is.as$date,-140,110,"ISIS Inflicted")

p.sdf.is <- div.plotting(civil.sdf.is,divisive.sdf.is,"ISIS Territory",time=civil.sdf.is$date,-140,110,"SDF-Inflicted") + theme(legend.position = "none")
p.is.sdf <- div.plotting(civil.is.sdf,divisive.is.sdf,"SDF Territory",time=civil.is.sdf$date,-140,110,"ISIS Inflicted")
 

mylegend<-g_legend(p.op.as)

B <- grid.arrange(arrangeGrob(p.as.op,p.op.as+ theme(legend.position = "none"), 
                              p.as.is,p.is.as+ theme(legend.position = "none"),
                              p.sdf.is,p.is.sdf+ theme(legend.position = "none"),ncol=2),mylegend, nrow=2,heights=c(10, 1))

library(extrafont) 
font_import()
loadfonts()

ggsave("15days_all.pdf", plot = B ,width=9.83, height=9,units = "in")  

###################
##
## Civilian and Combatant Deaths & Meeting Dates
##
###################
 
civ.nonciv.plot <- function(mydata, area, side, name2, time, lower, upper){
  pl <- ggplot(mydata, aes(x=time)) +
    sapply(mydata$date[(1:1430)[agree$agree==1]], function(xint) geom_line(linetype = "dashed", data = data.frame(x = c(xint,xint), y = c(-140, 110)), aes(x = x, y = y, color="Peace Talks"))) +
    geom_line(aes(y=side, color="Non-Civilian")) +
    geom_line(aes(y=-deaths_civilians, color="Civilian")) +  ggtitle(area) +  theme_classic() + 
    scale_y_continuous(name=name2, limits=c(lower, upper), labels=c(150,100,50,0,50,100)) + 
    labs(x = " ", y = name2, color = " ", size=0.8)  +
    scale_color_manual(name="", values = c("Non-Civilian"="palevioletred2", "Civilian"="skyblue2", "Peace Talks" = "black")) +
    scale_shape_manual(values = c(NA, NA, NA)) +
    theme(plot.title = element_text(size = 16, family = "LM Roman 10"), 
          axis.text=element_text(size=10, family = "LM Roman 10"),
          axis.title=element_text(size=12, family = "LM Roman 10"),
          legend.text=element_text(size=12, family = "LM Roman 10"),
          legend.title=element_text(size=12, family = "LM Roman 10"))
  return(pl)
}

pl.as.op <- civ.nonciv.plot(civil.as.op,"Opposition Territory",civil.as.op$deaths_b,"Government Inflicted",time=civil.as.op$date,-140,110)
pl.op.as <- civ.nonciv.plot(civil.op.as,"Government Territory",civil.op.as$deaths_a,"Opposition Inflicted",time=civil.op.as$date,-140,110) 

pl.as.is <- civ.nonciv.plot(civil.as.is,"Opposition Territory",civil.as.is$deaths_b,"Government Inflicted",time=civil.as.is$date,-140,110)
pl.is.as <- civ.nonciv.plot(civil.is.as,"Government Territory",civil.is.as$deaths_a,"Opposition Inflicted",time=civil.is.as$date,-140,110)

pl.sdf.is <- civ.nonciv.plot(civil.sdf.is,"Opposition Territory",civil.sdf.is$deaths_b,"Government Inflicted",time=civil.sdf.is$date,-140,110)
pl.is.sdf <- civ.nonciv.plot(civil.is.sdf,"Government Territory",civil.is.sdf$deaths_a,"Opposition Inflicted",time=civil.is.sdf$date,-140,110)
 

g_legend<-function(a.gplot){
  tmp <- ggplot_gtable(ggplot_build(a.gplot))
  leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
  legend <- tmp$grobs[[leg]]
  return(legend)}

mylegend<-g_legend(pl.op.as)

C <- grid.arrange(arrangeGrob(pl.as.op+ theme(legend.position = "none"),pl.op.as+ theme(legend.position = "none"), 
                              pl.as.is+ theme(legend.position = "none"),pl.is.as+ theme(legend.position = "none"),
                              pl.sdf.is+ theme(legend.position = "none"),pl.is.sdf+ theme(legend.position = "none"), ncol=2),mylegend, nrow=2,heights=c(8, 1))

ggsave("Civnonciv_ALL.pdf", plot =C ,width=9.83, height=9,units = "in") 

#########
### Both change and meeting dates
### Appendix

div.plotting <- function(mydata, divdata, area, time, lower, upper,y){
  ggplot(mydata, aes(x=time, y=d)) +
    geom_line() + 
    sapply(mydata$date[divdata[-c(1,length(divdata))]], function(xint2) geom_vline(aes(xintercept = xint2, color="Change Point"),linetype = "dashed")) +
    sapply(mydata$date[(1:1430)[agree$agree==1]], function(xint) geom_vline(aes(xintercept = xint, color="Meeting"),linetype = "dashed")) + 
    ggtitle(area) +  theme_classic() + 
    scale_y_continuous(name=y, limits=c(lower, upper)) + 
    labs(x = " ", y = y, color = " ", size=0.8) +
    scale_color_manual(name="", values = c("Change Point"="palevioletred2", "Meeting"="skyblue2")) +
    theme(plot.title = element_text(size = 16, family = "LM Roman 10"), 
          axis.text=element_text(size=10, family = "LM Roman 10"),
          axis.title=element_text(size=12, family = "LM Roman 10"),
          legend.text=element_text(size=12, family = "LM Roman 10"),
          legend.title=element_text(size=12, family = "LM Roman 10"))
}

p.as.op <- div.plotting(civil.as.op,divisive.as.op,"Opposition Territory",time=civil.as.op$date,-140,110, "Government-Inflicted") + theme(legend.position = "none")
p.op.as <- div.plotting(civil.op.as,divisive.op.as,"Government Territory",time=civil.op.as$date,-140,110,"Opposition-Inflicted")+theme(legend.direction = "horizontal")

p.as.is <- div.plotting(civil.as.is,divisive.as.is,"ISIS Territory",time=civil.as.is$date,-140,110, "Government-Inflicted") + theme(legend.position = "none")
p.is.as <- div.plotting(civil.is.as,divisive.is.as,"Government Territory",time=civil.is.as$date,-140,110,"ISIS Inflicted")

p.sdf.is <- div.plotting(civil.sdf.is,divisive.sdf.is,"ISIS Territory",time=civil.sdf.is$date,-140,110,"SDF-Inflicted") + theme(legend.position = "none")
p.is.sdf <- div.plotting(civil.is.sdf,divisive.is.sdf,"SDF Territory",time=civil.is.sdf$date,-140,110,"ISIS Inflicted") 

mylegend<-g_legend(p.op.as)

B <- grid.arrange(arrangeGrob(p.as.op,p.op.as+ theme(legend.position = "none"), 
                              p.as.is,p.is.as+ theme(legend.position = "none"),
                              #                              p.op.sdf,p.sdf.op+ theme(legend.position = "none"),
                              p.sdf.is,p.is.sdf+ theme(legend.position = "none"),ncol=2),mylegend, nrow=2,heights=c(10, 1))

ggsave("both_meeting_change.pdf", plot = B, width=9.83, height=9,units = "in")  
